package com.nuaa.service.overseas.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.nuaa.dao.overseas.inter.LocalDisDaoInter;
import com.nuaa.dao.repertory.inter.SubProDaoInter;
import com.nuaa.domain.overseas.LocalDisEntity;
import com.nuaa.domain.repertory.SubProductInfoEntity;
import com.nuaa.formmodel.LocalHandForm;
import com.nuaa.service.overseas.inter.LocalDisServiceInter;
import com.nuaa.utils.AjaxMsg;
import com.nuaa.utils.BusinessException;
import com.nuaa.utils.CustomCodeUtil;
import com.nuaa.utils.DataGrid;
import com.nuaa.utils.PageInfo;
/**
 * 本地配送订单
 * @author heaven
 *
 */
@Service("localDisService")
public class LocalDisServiceImpl implements LocalDisServiceInter{
	@Resource(name="localDisDao")
	private LocalDisDaoInter localDisDao;
	@Resource(name="subProInfoDao")
	private SubProDaoInter subProInfoDao;
	/**提交本地配送订单*/
	@Transactional
	@Override
	public AjaxMsg subLocalOrder(LocalDisEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		try{
			String LO_NO = CustomCodeUtil.getLocalDisOrderNo(localDisDao.getMaxId(entity.getClass()));
			entity.setOrder_no(LO_NO);
			entity.setCreate_tm(new Date());
			entity.setBusiness_type("海外仓本地配送订单");
			this.localDisDao.save(entity);
			msg.setSuccess(true);
			msg.setResult(LO_NO);
			msg.setMsg("提交订单成功！");
		}catch(Exception e){
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("提交失败！");
		}
		return msg;
	}
	/**
	 * 获取所有本地配送订单信息
	 */
	@Override
	public DataGrid<Map<String, Object>> getLocalDisOrders(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" where customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public DataGrid<Map<String, Object>> getWaitLocalDisOrders(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order where state = 0 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	
	@Override
	public DataGrid<Map<String, Object>> getProceLocalDisOrders(PageInfo pager, String customer_no, String orderNo,
			String tmFrom, String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order where state > 0 and state<5 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public DataGrid<Map<String, Object>> getFinishLocalDisOrders(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order where state = 5 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public DataGrid<Map<String, Object>> getNoDeliveredOrders(PageInfo pager, String customer_no, String orderNo,
			String tmFrom, String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order where  state<4 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	@Transactional
	public void update(LocalDisEntity lcoalOrder) {
		localDisDao.update(lcoalOrder);
		
	}
	
	@Override
	public LocalDisEntity getById(Integer id) {
		return localDisDao.find(LocalDisEntity.class, id);
	}
	@Override
	public DataGrid<Map<String, Object>> getDeliverOrders(PageInfo pager) {
		StringBuilder sql=new StringBuilder();
		sql.append("select customer_no,count(*) as order_total from local_dis_order where state<4  group by customer_no  ");
		
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	
	@Override
	public DataGrid<Map<String, Object>> getNonDelivaryLocalOrders(PageInfo pager) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order where state<4 ");
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public DataGrid<Map<String, Object>> getLocalDisUncheck(PageInfo pager) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order where state = 0 ");
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}

}
	/**
	 * 获取所有已发货订单
	 */
	@Override
	public DataGrid<Map<String, Object>> getDeliveredOrders(PageInfo pager) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order where state>=4 ");
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public DataGrid<Map<String, Object>> getLocalNoDeliveredDetail(
			PageInfo pager, String customer_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from local_dis_order ");
		if(!customer_no.equals("")){
			sql.append(" where customer_no ='");
			sql.append(customer_no);
			sql.append("' and state<4 ");
		}
		
		try{
			long total=localDisDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=localDisDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	/**
	 * 对指定用户编码下所有本地未配送订单进行发货
	 */
	@Transactional
	@Override
	public AjaxMsg deliverOrders(String customerNo) {
		AjaxMsg msg = new AjaxMsg();
		try{
			String sql = "update local_dis_order set state = 4 where customer_no = '"+customerNo+"'";
			localDisDao.update(sql);
			msg.setSuccess(true);
			msg.setMsg("发货提交完成！");
		}catch(Exception e){
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("发货提交失败！");
		}
		return msg;
	}
	/**
	 * 根据订单号获取订单信息
	 */
	@Override
	public LocalDisEntity getByOrderNo(String orderNo) {
		String sql = "select * from local_dis_order where order_no = '"+orderNo+"'";
		List<LocalDisEntity> list = this.localDisDao.search(LocalDisEntity.class, sql);
		return list.get(0);
	}
	@Transactional
	@Override
	public AjaxMsg saveLocalModOrder(LocalDisEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		LocalDisEntity retEntity = localDisDao.find(LocalDisEntity.class, entity.getId());
		retEntity.setDis_address(entity.getDis_address());
		retEntity.setPro_number(entity.getPro_number());
		retEntity.setReceiver(entity.getReceiver());
		retEntity.setLogiCompany(entity.getLogiCompany());
		retEntity.setLogiNumber(entity.getLogiNumber());
		try{
			localDisDao.save(retEntity);
			msg.setSuccess(true);
			msg.setMsg("修改成功！");
		}catch (Exception e) {
			msg.setSuccess(false);
			msg.setMsg("修改失败！");
		}
		return msg;
	}
	@Transactional
	@Override
	public AjaxMsg saveHand(String id,LocalHandForm form) {
		AjaxMsg msg = new AjaxMsg();
		try{
			LocalDisEntity entity = localDisDao.find(LocalDisEntity.class,Integer.parseInt(id));
			entity.setState(form.getState());
			entity.setLaborFee(form.getLaborFee());
			entity.setPostage(form.getPostage());
			localDisDao.update(entity);
			msg.setSuccess(true);
			msg.setMsg("操作成功！");
			/**
			 * 下面对库存中的商品数量进行处理
			 */
			if(form.getState()==5){//在处于完成状态
				String sql = "select * from sub_proInfo_tab where pro_no ='"+entity.getPro_no()+"'";
				List<SubProductInfoEntity> list = subProInfoDao.search(SubProductInfoEntity.class, sql);
				if(list.size()==1){
					SubProductInfoEntity curSubProInfo = list.get(0);
					int curNum = curSubProInfo.getPro_number();
					curSubProInfo.setPro_number(curNum-entity.getPro_number());
					subProInfoDao.update(curSubProInfo);
				}
			}
		}catch (Exception e) {
			msg.setSuccess(false);
			msg.setMsg("操作失败！");
		}
		return msg;
	}
}